#!/bin/sh
#
# Usage:
#   ./2-tools.sh <function name>

set -o nounset

# http://www.linuxfromscratch.org/lfs/view/stable/chapter05/introduction.html
#
# "There are two steps in building this minimal system. The first step is to
# build a new and host-independent toolchain (compiler, assembler, linker,
# libraries, and a few useful utilities). The second step uses this toolchain
# to build the other essential tools. "

LFS=/home/lfs

binutils1() {
  cd $LFS/sources/binutils-2.23.2  # no pushd in shell?

  sed -i -e 's/@colophon/@@colophon/' \
         -e 's/doc@cygnus.com/doc@@cygnus.com/' bfd/doc/bfd.texinfo

  mkdir -v ../binutils-build
  cd ../binutils-build

  time ../binutils-2.23.2/configure   \
      --prefix=/tools            \
      --with-sysroot=$LFS        \
      --with-lib-path=/tools/lib \
      --target=$LFS_TGT          \
      --disable-nls              \
      --disable-werror

  make

  case $(uname -m) in
    x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
  esac

  make install
}

# used by 3-tools.sh/gcc2

untar_gcc_deps() {
  # Decompress deps in this dir
  if ! test -d mpfr; then
    tar -xf $LFS/sources/mpfr-3.1.2.tar.xz
    mv -v mpfr-3.1.2 mpfr
  fi

  if ! test -d gmp; then
    tar -xf $LFS/sources/gmp-5.1.2.tar.xz
    mv -v gmp-5.1.2 gmp
  fi

  if ! test -d mpc; then
    tar -xf $LFS/sources/mpc-1.0.1.tar.gz
    mv -v mpc-1.0.1 mpc
  fi
}


# used by 3-tools.sh/gcc2

patch_gcc_1() {
  # Need to change some prefixes to support /tools
  for file in \
   $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h)
  do
    cp -uv $file $file.orig
    sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
        -e 's@/usr@/tools@g' $file.orig > $file
    echo '
  #undef STANDARD_STARTFILE_PREFIX_1
  #undef STANDARD_STARTFILE_PREFIX_2
  #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
  #define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
    touch $file.orig
  done
}


gcc1() {
  maybe_untar_cd gcc-4.8.1 .tar.bz2

  untar_gcc_deps

  set -o errexit

  patch_gcc_1

  sed -i '/k prot/agcc_cv_libc_provides_ssp=yes' gcc/configure

  mkdir_cd ../gcc-build

  ../gcc-4.8.1/configure                               \
      --target=$LFS_TGT                                \
      --prefix=/tools                                  \
      --with-sysroot=$LFS                              \
      --with-newlib                                    \
      --without-headers                                \
      --with-local-prefix=/tools                       \
      --with-native-system-header-dir=/tools/include   \
      --disable-nls                                    \
      --disable-shared                                 \
      --disable-multilib                               \
      --disable-decimal-float                          \
      --disable-threads                                \
      --disable-libatomic                              \
      --disable-libgomp                                \
      --disable-libitm                                 \
      --disable-libmudflap                             \
      --disable-libquadmath                            \
      --disable-libsanitizer                           \
      --disable-libssp                                 \
      --disable-libstdc++-v3                           \
      --enable-languages=c,c++                         \
      --with-mpfr-include=$(pwd)/../gcc-4.8.1/mpfr/src \
      --with-mpfr-lib=$(pwd)/mpfr/src/.libs

  make
  make install

  ln -sv libgcc.a $($LFS_TGT-gcc -print-libgcc-file-name | sed 's/libgcc/&_eh/')
}

if test $(basename $0) = 2-tools.sh; then
  "$@"
fi
